package tsinghua.smxq.mc;

import java.util.Calendar;
import java.util.Locale;

import tsinghua.smxq.ui.MCListViewListener;
import tsinghua.smxq.ui.MCMenuAdapter;
import tsinghua.smxq.ui.MCMenuView;
import tsinghua.smxq.utilitiespayment.CustomerDatePickerDialog;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class ReqActivity extends Activity {

	public MCMenuView menuView;
	private ListView menuList;
	private View RightView;
	private View[] children;
	private MCMenuAdapter menuAdapter;
	private MCListViewListener menuListener;
	Button menuBtn;
	LayoutInflater inflater;

	Button start;
	RadioGroup timechoosegroup, typechoosegroup;
	RadioButton allrecords_radio, onemonth_radio, water_radio, elec_radio;
	DatePicker datepicker;
	private Toast toast;

	int year;
	int month;
	int day;
	Calendar calendar;
	int selectedYear;
	int selectedMonth;
	boolean isAll = true;
	int type = 0; // 0表示水费，1表示电费

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		inflater = LayoutInflater.from(this);
		setContentView(inflater.inflate(R.layout.menu_scroll_view, null));
		RightView = inflater.inflate(R.layout.activity_req, null);
		menuView = (MCMenuView) findViewById(R.id.mScrollView);
		menuAdapter = new MCMenuAdapter(this);

		menuList = (ListView) findViewById(R.id.menuList);
		menuListener = new MCListViewListener(this);
		menuList.setAdapter(menuAdapter);
		menuList.setOnItemClickListener(menuListener);
		menuBtn = (Button) RightView.findViewById(R.id.utilities_menuBtn);
		menuBtn.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ReqActivity.this.menuView.clickMenuBtn();
			}
		});

		typechoosegroup = (RadioGroup) RightView
				.findViewById(R.id.type_radiogroup_req);
		water_radio = (RadioButton) RightView.findViewById(R.id.water_radio);
		timechoosegroup = (RadioGroup) RightView
				.findViewById(R.id.choose_radiogroup_req);
		allrecords_radio = (RadioButton) RightView
				.findViewById(R.id.allrecords_radio);
		onemonth_radio = (RadioButton) RightView
				.findViewById(R.id.onemonth_radio);
		start = (Button) RightView.findViewById(R.id.start_button_req);

		typechoosegroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (checkedId == water_radio.getId()) {
							DisplayToast("您选择了水费记录", Toast.LENGTH_SHORT);
							type = 0;
						} else {
							DisplayToast("您选择了电费记录", Toast.LENGTH_SHORT);
							type = 1;
						}
					}
				});

		timechoosegroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (checkedId == allrecords_radio.getId()) {
							DisplayToast("您选择了查询所有记录", Toast.LENGTH_SHORT);
							isAll = true;
							start.setClickable(true);
						} else {
							isAll = false;
							showDatePickerDialog();
						}
					}
				});

		start.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				if (isAll) {
					Intent gotoIntent = new Intent(ReqActivity.this,
							RecordsActivity.class);
					gotoIntent.putExtra("type", type);
					gotoIntent.putExtra("isAll", true);
					ReqActivity.this.startActivity(gotoIntent);
				} else {
					Intent gotoIntent = new Intent(ReqActivity.this,
							RecordsActivity.class);
					gotoIntent.putExtra("type", type);
					gotoIntent.putExtra("isAll", false);
					gotoIntent.putExtra("month", month);
					ReqActivity.this.startActivity(gotoIntent);
				}
			}
		});

		View leftView = new View(this);
		children = new View[] { leftView, RightView };
		leftView.setBackgroundColor(Color.TRANSPARENT);
		this.menuView.initViews(children, this.menuBtn, this.menuList);
		this.menuView.setMenuBtn(this.menuBtn);
		MCMenuAdapter.currPosition = 6;

	}

	// 显示时间选择对话框
	public void showDatePickerDialog() {
		start.setClickable(false);
		calendar = Calendar.getInstance(Locale.CHINA);
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH);
		day = calendar.get(Calendar.DAY_OF_MONTH);
		DatePickerDialog datePickerDialog = new CustomerDatePickerDialog(this,
				listener, calendar.get(Calendar.YEAR),
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
		datePickerDialog.setTitle(calendar.get(Calendar.YEAR) + "年"
				+ calendar.get(Calendar.MONTH + 1) + "月");
		datePickerDialog.show();
		DatePicker dp = findDatePicker((ViewGroup) datePickerDialog.getWindow()
				.getDecorView());
		if (dp != null) {
			((ViewGroup) dp.getChildAt(0)).getChildAt(2).setVisibility(
					View.GONE);
		}
	}

	private DatePicker findDatePicker(ViewGroup group) {
		if (group != null) {
			for (int i = 0, j = group.getChildCount(); i < j; i++) {
				View child = group.getChildAt(i);
				if (child instanceof DatePicker) {
					return (DatePicker) child;
				} else if (child instanceof ViewGroup) {
					DatePicker result = findDatePicker((ViewGroup) child);
					if (result != null)
						return result;
				}
			}
		}
		return null;

	}

	DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			selectedYear = year;
			selectedMonth = monthOfYear + 1;
			DisplayToast(year + "年" + selectedMonth + "月", Toast.LENGTH_SHORT);
			start.setClickable(true);
		}
	};

	public void DisplayToast(String str, int time) {
		toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
		// 设置toast显示的位置
		toast.setGravity(Gravity.TOP, 0, 320);
		// 显示该Toast
		toast.show();
	}

}
